package com.huanying.goodjob.skill;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.MemCachedManager;
import com.huanying.goodjob.community.Area;
import com.huanying.goodjob.jobtype.Jobtype;

@Service
public class SkillServiceImpl implements SkillService {

	@Autowired
	private BaseDao dao;
	 private MemCachedManager cache ;
	    private Map<String,Object> map;
	@Override
	public void add_skill(Skill skill) throws Exception {
		dao.save(skill);
		map=new HashMap<String,Object>();
		cache=MemCachedManager.getInstance();
          if(cache.get("skill")==null)
          {
        	  map.put("skill_id"+skill.getId(),skill);
        	  cache.set("skill", map);
          }else
          {
        	  map=(Map<String,Object>)cache.get("skill");
        	  map.put("skill_id"+skill.getId(),skill);
        	  cache.set("skill", map);
          }
	}

	@Override
	public void update_skill(Skill skill) throws Exception {
		// TODO Auto-generated method stub
		dao.update(skill);
		cache=MemCachedManager.getInstance();
		map=new HashMap<String,Object>();
		 if(cache.get("skill")!=null)
         {
			  map=(Map<String,Object>)cache.get("skill");
        	  map.replace("skill_id"+skill.getId(),skill);
        	  cache.set("skill", map);
         }
	}

	@Override
	public int delete_skill(int id) throws Exception {
		// TODO Auto-generated method stub
		List<Object> param = new ArrayList<Object>();
		String hql = "update Skill set status=0 where id=?";
		param.add(id);
		return dao.exculeByParams(hql, param);
	}

	@Override
	public PageBean search_skill(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from Skill as s where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("name") != "" && map.get("name") != null) {
			hql.append(" and s.name like ?");
			param.add("%" + map.get("name") + "%");
		}
		if (map.get("jobtype") != "" && map.get("jobtype") != null) {
			hql.append(" and s.jobtype.id=?");
			param.add(Integer.parseInt((String) map.get("jobtype")));
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public Skill getbyId_skill(int id) throws Exception {

		return (Skill) dao.load(Skill.class, id);
	}

	@Override
	public List<Jobtype> get_jobtype() throws Exception {
		// TODO Auto-generated method stub
		String hql = "from Jobtype where 1=1";
		Session session = null;
		session = dao.getSession();
		Query query = session.createQuery(hql);
		return (List<Jobtype>) query.list();
	}

	@Override
	public List search__skillbyjobtypeId(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Skill as s where s.jobtype.id=? and status=1";
		List<Object> param = new ArrayList<Object>();
		param.add(id);
		return dao.exculeHqlByParams(hql, param);
	}

	@Override
	public List get_allSkill() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Skill as s where status=1";
		return dao.exculeHqlByParams(hql, null);
	}
}
